package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivityCollectVideoBinding;
import flc.ast.dialog.CollectEditDialog;
import flc.ast.dialog.InfoDialog;
import flc.ast.dialog.PriEditDialog;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;

public class CollectVideoActivity extends BaseAc<ActivityCollectVideoBinding> {

    private SelVideoAdapter videoAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_collect_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCollectVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext));
        videoAdapter = new SelVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.addChildClickViewIds(R.id.ivSelVideoItemEdit);
        videoAdapter.setOnItemChildClickListener(this);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PlayActivity.listPlay = videoAdapter.getValidData();
        PlayActivity.type = 2;
        PlayActivity.selPlayPos = position;
        startActivity(PlayActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivSelVideoItemEdit://编辑
                showEditDialog(position);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<SelectMediaEntity> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            videoAdapter.setList(listSp);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showEditDialog(int position) {
        CollectEditDialog dialog = new CollectEditDialog(mContext);
        dialog.selectMediaEntity = listShow.get(position);
        dialog.setListener(new CollectEditDialog.Ilistener() {
            @Override
            public void clickCollect() {
                getData();
            }

            @Override
            public void clickShare() {
                IntentUtil.shareVideo(mContext, listShow.get(position).getPath());
            }

            @Override
            public void clickAddPrivacyFolder() {
                String filePath = FileUtil.generateFilePath(Content.myPrivacyVideo, "." + FileUtils.getFileExtension(listShow.get(position).getPath()));
                FileUtils.copy(listShow.get(position).getPath(), filePath);
                ToastUtils.showShort(getString(R.string.add_suc));
            }

            @Override
            public void clickInfo() {
                showInfoDialog(position);
            }
        });
        dialog.show();
    }

    private void showInfoDialog(int position) {
        InfoDialog dialog = new InfoDialog(mContext);
        dialog.videoPath = listShow.get(position).getPath();
        dialog.show();
    }

}